<?php
	$ls_gridname	= 'dtl_jadwal';


// ----------------- >> Form Detail << ----------------- ?>

<script>
	console.clear();
	var idJadwal = $('#id_jadwal_pk').val();
	var listShift = <?php echo json_encode($listShift) ?>;

	function comboFormatter(value){
		for(var i=0; i<listShift.length; i++){
			if (listShift[i].value == value) {
				return listShift[i].display;
			}
		}
		return value;
	}

	function simpan_detail_jadwal() {
        $('#<?php echo $ls_gridname; ?>').datagrid('acceptChanges');
        var listData = $('#<?php echo $ls_gridname; ?>').datagrid('getData');
        $.each(listData.rows, function( index, value ) {
            if (value.id_shift_fk == null) { val_shift = ''; }
            else { val_shift = value.id_shift_fk; }
            $.ajax({
                url: gs_path+'/'+controller+'/edit_detail',
                type:'POST',
                data: {
                    id_detail_jadwal_pk : value.id_detail_jadwal_pk,
                    id_shift_fk : val_shift
                }, success:function(data) {
                    console.log("berhasil");
                }, error: function(errorThrown) { 
                   console.log("gagal");
                }  
            });
        });
    }

	$(function(){
		var lastIndex;
		$('#<?php echo $ls_gridname; ?>').datagrid({
			url:gs_path+'/'+controller+'/getDetailJadwal/'+$('#id_jadwal_pk').val(),
			toolbar:[
				{
					text:'accept',
					iconCls:'icon-save',
					handler:function(){
						simpan_detail_jadwal();
						$.messager.alert('Info','Detail jadwal Berhasil Di simpan!','info');
					}
				},'-',{
					text:'reject',
					iconCls:'icon-undo',
					handler:function(){
						$('#<?php echo $ls_gridname; ?>').datagrid('rejectChanges');
						// alert("gagal");
					}
				}
			],
			onBeforeLoad:function(){
				$(this).datagrid('rejectChanges');
			},
			onClickRow:function(rowIndex){
				if (lastIndex != rowIndex){
					$('#<?php echo $ls_gridname; ?>').datagrid('endEdit', lastIndex);
					$('#<?php echo $ls_gridname; ?>').datagrid('beginEdit', rowIndex);
				}
				lastIndex = rowIndex;
			},
			rowStyler:function(index,row,css){
				if (row.hari=='Minggu'){
					return 'background-color:#FF0080;color:#fff;font-weight:bold;';
				}
			}
		});
	});
</script>


<table id="<?php echo $ls_gridname; ?>" style="width:700px;height:auto" iconCls="icon-edit" singleSelect="true" idField="itemid">
	<thead>
		<tr>
			<th field="id_detail_jadwal_pk" width="150" > ID</th>
	        <th field="tgl_jadwal" width="150" >Tanggal Jadwal</th>
	        <th field="hari" width="100" >Hari</th>
			<th field="id_shift_fk" width="200" formatter="comboFormatter" editor="{type:'combobox',options:{valueField:'value',textField:'display',data:listShift}}">Shift</th>
	        <!-- <th field="jam_mulai" width="100" >Mulai</th>
	        <th field="jam_selesai" width="100" >Selesai</th> -->
		</tr>
	</thead>
</table>